package com.study.mynew.ui;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;

import com.study.mynew.R;
import com.study.mynew.bean.NewItemBean;
import com.study.mynew.bean.NewsData;
import com.study.mynew.utils.StatusBarUtils;

import org.litepal.LitePal;

import java.util.List;


public class NewsActivity extends AppCompatActivity {
    public static String EXTRA_DATA = "EXTRA_DATA";
    private WebView webView;
    Boolean isCollected;
    private ImageButton collectBtn,backBtn;
        private  NewItemBean bean ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.setWindowStatusBarColor(this, R.color.transparent);
        Bundle bundle = getIntent().getExtras();
        setContentView(R.layout.activity_news);
        backBtn = findViewById(R.id.newsBackBtn);
        collectBtn = findViewById(R.id.collectButton);

        bean =   (NewItemBean)bundle.getSerializable(EXTRA_DATA);
        webView = findViewById(R.id.webview_new);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(bean.getUrl());
        isCollected =  checkIsExist(bean.getUrl());
        if(isCollected) {
            collectBtn.setBackground(getResources().getDrawable(R.drawable.collected));
        } else {
            collectBtn.setBackground(getResources().getDrawable(R.drawable.collect));
        }
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if(!checkNewIsRead(bean.getUniquekey())) {
            bean.setFromCollection(false);
            bean.save();
        }
        collectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isCollected) {
                    collectBtn.setBackground(getResources().getDrawable(R.drawable.collected));
                    isCollected = true;
                    bean.setFromCollection(true);
                    bean.save();
                    Toast.makeText(NewsActivity.this, "收藏成功！", Toast.LENGTH_SHORT).show();
                } else {
                    collectBtn.setBackground(getResources().getDrawable(R.drawable.collect));
                    isCollected = false;
                    LitePal.deleteAll(NewItemBean.class,"url = ?&fromCollection = 1",bean.getUrl());
                    Toast.makeText(NewsActivity.this, "取消收藏成功！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    private boolean checkIsExist(String url) {
        NewItemBean first = LitePal.where("url = ?", url).findFirst(NewItemBean.class);
        return first != null;
    }

    private boolean checkNewIsRead(String id){
       return LitePal.where("uniquekey = ? & fromCollection = 0",id).findFirst(NewItemBean.class) != null;
    }

}